/**
 * Created by linxh on 2015/7/7.
 */
define("framework/dropdown/GillionDropItemDirectiveConstructor",['angular'],function(angular){
    return function($parse){

        function DropdownItem(scope,element,attrs){
            this.scope = scope;
            this.element = element;
            this.attrs = attrs;
        };

        DropdownItem.prototype.select = function() {
            this.element.attr("selected","selected");
            this.scope.isSelected = true;
            //this.scope.$digest();
        };

        DropdownItem.prototype.unSelect = function() {
            this.element.removeAttr("selected");
            this.scope.isSelected = false;
            //this.scope.$digest();
        };


        DropdownItem.prototype.getSelect = function(){
            return !!this.scope.isSelected;
        };

        DropdownItem.prototype.addSelectStyle = function(){
            this.element.addClass('current');
        };

        DropdownItem.prototype.removeSelectStyle = function(){
            this.element.removeClass('current');
        };

        return{
            restrict:"E",
            //template:'<a ng-class="{\'select-down-a\':true,\'ellipsis\':!isRadio,\'radio-effect\':isRadio}" href="javascript:void(0);">' +
            //          '<i ng-show="listType" ng-class="{\'check-box-icon\':isCheckbox,\'iconfont\':true,\'icon-check\':isCheckbox&&!isSelected,\'icon-radio\':isRadio&&!isSelected,\'radio-box-icon\':isRadio,\'icon-radioed\':isRadio&&isSelected,\'icon-checked\':isCheckbox&&isSelected}" >' +
            //          '</i><span ng-transclude></span></a>',
            template:'<li ><a href="javascript:void(0)"><i class="fi" ng-class="{\'fi-checkbox\':isCheckbox,\'fi-radiobox\':isRadio}"></i><span ng-transclude></span></a></li>',
            replace: true,
            transclude:true,
            scope:{
            },
            require: "^?gDropdown",
            link:function(scope, element,attrs, controller){
                scope.listType = controller.$scope.listType;
                scope.displayExpress = controller.$scope.displayExpress;
                scope.valueSeparator = controller.valueSeparator;
                scope.listType=='single'?scope.isRadio = true:scope.isRadio = false;
                scope.listType=='multi'?scope.isCheckbox = true:scope.isCheckbox = false;
                if(attrs.text){
                    element.find("a").append(attrs.text);
                }
                var dropItem = new DropdownItem(scope,element,attrs);
                controller.itemList.push(dropItem);


                //if(controller.itemList.length>9 && !controller.$scope.scrollY){
                //    controller.$scope.scrollY =true;
                //}

                element.on("click",function(){
                    controller.selectItem(dropItem);
                });
            }
        }
    }
})